package com.knife.bbs.action;

import com.knife.bbs.business.BBSUserService;
import com.knife.bbs.business.SkinService;
import com.knife.bbs.object.Skin;
import com.knife.util.CommUtil;
import com.knife.util.MD5;
import com.knife.web.IWebAction;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;

public class ActiveAction implements IWebAction {
	
	private SkinService skinService = SkinService.getInstance();
	private BBSUserService userService = new BBSUserService();
	
	public String skin="default";
	
	/**
	 * 激活用户
	 * @param form
	 * @param module
	 * @return
	 */
	public Page execute(WebForm form, Module module) throws Exception {
		Skin myskin=skinService.getUsedSkin();
		if(myskin!=null){
			skin=myskin.getSkin();
		}
		
		String userName = CommUtil.null2String(form.get("username"));
		com.knife.bbs.domain.UserInfo userInfo = new com.knife.bbs.domain.UserInfo();
		userInfo.setUsername(userName);
		userInfo.setPassword(MD5.encode(CommUtil.null2String(form
				.get("password"))));
		userInfo.setEmail(CommUtil.null2String(form.get("email")));
		userInfo.setSex(CommUtil.null2String(form.get("sex")));
		userInfo.setTel(CommUtil.null2String(form.get("tel")));
		userInfo.setQq(CommUtil.null2String(form.get("qq")));
		userInfo.setIntro(CommUtil.null2String(form.get("intro")));
		userInfo.setPhoto(CommUtil.null2String(form.get("photo")));
		userInfo.setStatus(new Integer(0));
		if (userService.getUserInfoByName(userName) != null) {
			form.addResult("msg", "用户名已经存在！");
			return module.findPage("/"+skin+"/","edit");
		}
		if (userService.add(userInfo)) {
			form.addResult("msg", "用户添加成功！");
		} else {
			form.addResult("msg", "用户添加失败！");
			return module.findPage("/"+skin+"/","edit");
		}
		//return doQuery(form, module);
		return new Page("clientforward", "/bbs/index.do", "url");
	}
}
